package com.example.elevator_cal

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import androidx.appcompat.app.AlertDialog
import kotlin.math.abs


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn.setOnClickListener {
            if (!kg.text.toString().trim().equals("") && !height.text.toString().trim().equals("")) {
                if (rb1.isChecked || rb2.isChecked) {
                    val wt = kg.text.toString().toFloat()
                    val ht = height.text.toString().toFloat()

                    val subLt = arrayOf(80, 70)
                    val rateLt = arrayOf(0.7, 0.6)
                    var subValue = 0
                    if (rb2.isChecked) {
                        subValue = 1
                    }
                    val dreamWeight = (ht - subLt[subValue]) * rateLt[subValue]
                    val overWeight = (wt - dreamWeight) / dreamWeight

                    println("dreamWeight: $dreamWeight $overWeight")

                    val tipList = arrayOf(
                        "体重不足, 体重偏轻20%以上", "体重过轻, 体重偏轻10%到20%",
                        "正常体重, 体重正负10%", "体重过重, 体重偏重10%到20%", "肥胖的小可爱，体重偏重20%以上"
                    )
                    var tipIndex = 2 // 初始设置为正常体重嘻嘻嘻
                    when {
                        overWeight < -0.2 -> tipIndex = 0
                        overWeight in -0.2..-0.1 -> tipIndex = 1
                        overWeight in 0.1..0.2 -> tipIndex = 3
                        overWeight > 0.2 -> tipIndex = 4
                    }
                    showResultView.text = tipList[tipIndex]
                } else {
                    showDialog("请选择性别") // 用AlertDialog实现函数
                }
            } else {
                showDialog("请输入身高或体重！") // 用AlertDialog实现函数    }
            }
        }
    }

    private fun showDialog(message_str: String) {
        val builder = AlertDialog.Builder(this)
        builder.setTitle("错误提示消息")
            .setMessage(message_str)
            .setPositiveButton("确定", { dialog, id -> })
            .setNegativeButton("取消", { dialog, id -> })

        builder.create()
        builder.show()
    }
}
